#include "StdAfx.h"
#include "YStorageCmd.h"
#include "..\DataLayer\YStorage.h"

YStorageCmd::YStorageCmd(void)
{
}

YStorageCmd::~YStorageCmd(void)
{
}

BOOL YStorageCmd::Add(YStorage* pYStorage)
{
	CString strSql = _T("");
	strSql.Format(_T("select * from ys_storage"));
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	if ( !pRecordset->IsEof() )
	{
		bRet = pRecordset->AddNew();
		if ( bRet )
		{
			CString strCode(pYStorage->GetCode());
			CString strName(pYStorage->GetName());
			CString strLocation(pYStorage->GetLocation());
			CString strMemo(pYStorage->GetMemo());
			strCode = SqlFilterToDb(strCode);
			strName = SqlFilterToDb(strName);
			strLocation = SqlFilterToDb(strLocation);
			strMemo = SqlFilterToDb(strMemo);

			pRecordset->SetFieldValue( _T("sr_code"),strCode);
			pRecordset->SetFieldValue( _T("sr_name"),strName);
			pRecordset->SetFieldValue( _T("sr_location"),strLocation);
			pRecordset->SetFieldValue( _T("sr_memo"),strMemo);
			bRet = pRecordset->Update();
			if ( bRet )
			{
				int nId = -1;
				pRecordset->GetFieldValue( _T("sr_id"),nId );
				pYStorage->SetId(nId);
			}
		}
	}
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
	return bRet;
}

BOOL YStorageCmd::DelByID(int nID)
{
	CString strSql = _T("");
	strSql.Format(_T("select * from ys_storage where sr_id=%d"),nID);
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	if ( !pRecordset->IsEof() )
	{
		bRet = pRecordset->Delete();
	}
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
	return bRet;
}

BOOL YStorageCmd::EditByID(YStorage* pYStorage)
{
	CString strSql = _T("");
	strSql.Format(_T("select * from ys_storage where sr_id=%d"),pYStorage->GetId());
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	if ( !pRecordset->IsEof() )
	{
		pRecordset->Edit();
		CString strCode(pYStorage->GetCode());
		CString strName(pYStorage->GetName());
		CString strLocation(pYStorage->GetLocation());
		CString strMemo(pYStorage->GetMemo());
		strCode = SqlFilterToDb(strCode);
		strName = SqlFilterToDb(strName);
		strLocation = SqlFilterToDb(strLocation);
		strMemo = SqlFilterToDb(strMemo);

		pRecordset->SetFieldValue( _T("sr_code"),strCode);
		pRecordset->SetFieldValue( _T("sr_name"),strName);
		pRecordset->SetFieldValue( _T("sr_location"),strLocation);
		pRecordset->SetFieldValue( _T("sr_memo"),strMemo);
		bRet = pRecordset->Update();
	}
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
	return bRet;
}

void YStorageCmd::GetByID(YStorage* pYStorage)
{
	CString strSql = _T("");
	strSql.Format(_T("select * from ys_storage where sr_id=%d"),pYStorage->GetId());
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	if ( !pRecordset->IsEof() )
	{
		int nId = -1;
		CString strCode = _T("");
		CString strName = _T("");
		CString strLocation = _T("");
		CString strMemo = _T("");
		pRecordset->GetFieldValue( _T("sr_id"),nId);
		pRecordset->GetFieldValue( _T("sr_code"),strCode);
		pRecordset->GetFieldValue( _T("sr_name"),strName);
		pRecordset->GetFieldValue( _T("sr_location"),strLocation);
		pRecordset->GetFieldValue( _T("sr_memo"),strMemo);
		strCode = SqlFilterToStr(strCode);
		strName = SqlFilterToStr(strName);
		strLocation = SqlFilterToStr(strLocation);
		strMemo = SqlFilterToStr(strMemo);

		pYStorage->SetId(nId);
		pYStorage->SetCode( (LPTSTR)(LPCTSTR)strCode );
		pYStorage->SetName( (LPTSTR)(LPCTSTR)strName );
		pYStorage->SetLocation( (LPTSTR)(LPCTSTR)strLocation );
		pYStorage->SetMemo( (LPTSTR)(LPCTSTR)strMemo );
	}
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
}

void YStorageCmd::Get(CTypedPtrList<CPtrList, YStorage *>*pRecList)
{
	CString strSql = _T("");
	strSql.Format(_T("select * from ys_storage"));
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	while ( !pRecordset->IsEof() )
	{
		YStorage* pYStorage = new YStorage;
		int nId = -1;
		CString strCode = _T("");
		CString strName = _T("");
		CString strLocation = _T("");
		CString strMemo = _T("");
		pRecordset->GetFieldValue( _T("sr_id"),nId);
		pRecordset->GetFieldValue( _T("sr_code"),strCode);
		pRecordset->GetFieldValue( _T("sr_name"),strName);
		pRecordset->GetFieldValue( _T("sr_location"),strLocation);
		pRecordset->GetFieldValue( _T("sr_memo"),strMemo);
		strCode = SqlFilterToStr(strCode);
		strName = SqlFilterToStr(strName);
		strLocation = SqlFilterToStr(strLocation);
		strMemo = SqlFilterToStr(strMemo);

		pYStorage->SetId(nId);
		pYStorage->SetCode( (LPTSTR)(LPCTSTR)strCode );
		pYStorage->SetName( (LPTSTR)(LPCTSTR)strName );
		pYStorage->SetLocation( (LPTSTR)(LPCTSTR)strLocation );
		pYStorage->SetMemo( (LPTSTR)(LPCTSTR)strMemo );
		pRecList->AddTail(pYStorage);

		pRecordset->MoveNext();
	}
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
}

int YStorageCmd::SameNameRecordCount(TCHAR* pName,int nID)
{
	CString strSql = _T("");
	CString strName = SqlFilterToDb(pName);
	strSql.Format(_T("select * from ys_storage where sr_name=\'%s\' and sr_id <> %d"),strName,nID);
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	bRet = pRecordset->GetRecordCount();
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
	return (int)bRet;
}

int YStorageCmd::SameNameRecordCount(TCHAR* pName)
{
	CString strSql = _T("");
	CString strName = SqlFilterToDb(pName);
	strSql.Format(_T("select * from ys_storage where sr_name=\'%s\'"),strName);
	CADORecordset* pRecordset = new CADORecordset();
	BOOL bRet = UDataBase::GetDataBase()->GetRecordData(pRecordset,strSql);
	bRet = pRecordset->GetRecordCount();
	pRecordset->Close();
	delete pRecordset;
	pRecordset = NULL;
	return (int)bRet;
}